package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(retries = 0,timeout = 10000)
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

   @Autowired
   private  CheckItemService checkItemService;

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        return baseMapper.findAll();
    }


    @Override
    public void saveCheckGroup(CheckGroupDTO checkGroupDTO) {

        Integer id = checkGroupDTO.getId();
        //  id   修改操作  值 不为null      insert  操作  为null
        if(!StringUtils.isEmpty(id)){
            //   修改操作   先删除  中间表数据
            baseMapper.deleteCheckItemIdsByGroupId(id);
        }
        //    检查组 信息录入  +  中间表数据录入
        saveOrUpdate(checkGroupDTO);  //    checkGroupDTO 存在id   update 检查组表信息修改
        id  = checkGroupDTO.getId();
        // 2  中间表录入数据
        for (Integer checkitemId : checkGroupDTO.getCheckitemIds()) {
            baseMapper.addCheckGroupIdAndCheckItemId(id,checkitemId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //  mp   配置插件：  完成分页 查询
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper();//   封装查询条件
        queryWrapper.eq("is_delete",0);
        Page<CheckGroup> page =null ;
       if(StringUtils.isEmpty(queryPageBean.getQueryString())){
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
       }else{
         //   select  * from  xxxx where  name like  or  code like  ? xxx  limit ?,?
           queryWrapper.and(i->i.like("name",queryPageBean.getQueryString()).or().like("HELPCODE",queryPageBean.getQueryString()));
           page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
       }
        PageResult result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public Map findCheckItemInfoByGroupId(int id) {
        Map map  = new HashMap();
        List<CheckItem> list = checkItemService.list();
        map.put("checkitems",list);
        //  根据检查组id  去中间表查询 对应检查项ids
        int[]  checkItemIds = baseMapper.findCheckItemIdsByGroupId(id);
        map.put("checkitemIds",checkItemIds);
        return map;
    }

    @Override
    public List<CheckItem> findCheckItemByGid(Integer id) {
        return baseMapper.findCheckItemByGid(id);
    }

}



